﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUParameterConfig.Class;

namespace RTUParameterConfig.Controls
{
    public partial class WaterLevelAndCurrentSpeeds : UserControl
    {
        public int intIndex;
        public int DeviceSelectNo;//设备选择序号
        public ParameterConfig mf;

        public WaterLevelAndCurrentSpeeds(ParameterConfig mfs)
        {
            InitializeComponent();
            mf = mfs;
        }

        /// <summary>
        /// 初始化参数
        /// </summary>
        /// <param name="type">设备类型</param>
        /// <param name="index">设备标识</param>
        /// <param name="intDeviceSelectNo">设备选择序号</param>
        public void InitValue(int type, int index, int intDeviceSelectNo)
        {
            DeviceSelectNo = intDeviceSelectNo;
            intIndex = index;
            this.Tag = type;
            this.TabIndex = (type + 1) * 10 + index;
        }

        /// <summary>
        /// 设置多流速配置
        /// </summary>
        public void SetValue()
        {
            sp1.Controls.Clear();
            WaterLevels wl = new WaterLevels(this);
            wl.InitValue(intIndex, 0);
            wl.SetValue();
            sp1.Controls.Add(wl);

            int index = 0;
            if (wl.cbbLy.SelectedIndex == 6 || wl.cbbLy.SelectedIndex == 7)
            {
                index = 1;
            }

            int count = Convert.ToUInt16(ParameterConfig.rtuConfigInfo.MultiSpeed_Nums);
            for (int i = index; i < count; i++)
            {
                CurrentSpeeds cs = new CurrentSpeeds();
                cs.InitValue(i, DeviceSelectNo);
                cs.SetValue();
                sp1.Controls.Add(cs);
            }
        }

        public void Add()
        {
            WaterLevels wl = new WaterLevels(this);
            wl.InitValue(intIndex, 0);
            //wl.SetValue();
            //wl.cbbLy.SelectedIndex = 0;
            sp1.Controls.Add(wl);
            AddCurrentSpeed();
            AddCurrentSpeed();
        }

        public void SetNo(int intWaterLevelDeviceSelect)
        {
            for (int i = 0; i < sp1.Controls.Count; i++)
            {
                if (sp1.Controls[i] is CurrentSpeeds)
                {
                    CurrentSpeeds cs = sp1.Controls[i] as CurrentSpeeds;
                    if (intWaterLevelDeviceSelect == 6 || intWaterLevelDeviceSelect == 7)
                    {
                        cs.InitValue(i, DeviceSelectNo);
                    }
                    else
                    {
                        cs.InitValue(i - 1, DeviceSelectNo);
                    }
                }
            }
        }

        /// <summary>
        /// 获取多流速配置
        /// </summary>
        public void GetValue()
        {
            ParameterConfig.rtuConfigInfo.MultiSpeed_Enable |= 0x0001 << 0;
            int count = 0;
            for (int i = 0; i < sp1.Controls.Count; i++)
            {
                if (sp1.Controls[i] is WaterLevels)
                {
                    WaterLevels wl = sp1.Controls[i] as WaterLevels;
                    wl.GetValue();
                    if (wl.cbbLy.SelectedIndex == 6 || wl.cbbLy.SelectedIndex == 7)
                    {
                        count++;
                    }
                }

                if (sp1.Controls[i] is CurrentSpeeds)
                {
                    CurrentSpeeds cs = sp1.Controls[i] as CurrentSpeeds;
                    cs.GetValue();
                    count++;
                }
            }
            ParameterConfig.rtuConfigInfo.MultiSpeed_Nums = Convert.ToByte(count);
        }

        /// <summary>
        /// 检查配置
        /// </summary>
        /// <returns>true,false</returns>
        public bool CheckConfig()
        {
            ArrayList listAddr = new ArrayList();
            for (int i = 0; i < sp1.Controls.Count; i++)
            {
                int WaterLevelDeviceSelect = 0;
                if (sp1.Controls[i] is WaterLevels)
                {
                    WaterLevels wl = sp1.Controls[i] as WaterLevels;            
                    listAddr.Add(wl.nud2.Value);
                    WaterLevelDeviceSelect = wl.cbbLy.SelectedIndex;
                }

                if (WaterLevelDeviceSelect == 6 || WaterLevelDeviceSelect == 7)
                {
                    if (DeviceSelectNo != 1)
                    {
                        Common.ShowErrorMessage("多流速仪设备选择" + Common.CurrentSpeedList1[DeviceSelectNo].ToString() + ",水位计设备不能选择" + Common.WaterLevelsList[WaterLevelDeviceSelect].ToString() + "！");
                        return false;
                    }
                    else
                    {
                        if (sp1.Controls.Count == 6)
                        {
                            Common.ShowErrorMessage("水位设备选择" + Common.WaterLevelsList[WaterLevelDeviceSelect].ToString() + "已有流速，请删除一个流速！");
                            return false;
                        }
                    }
                }


                if (sp1.Controls[i] is CurrentSpeeds)
                {
                    CurrentSpeeds cs = sp1.Controls[i] as CurrentSpeeds;
                    if (!listAddr.Contains(cs.nud11.Value))
                    {
                        listAddr.Add(cs.nud11.Value);
                    }
                    else
                    {
                        Common.ShowErrorMessage("设备通讯地址重复！");
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 添加流速
        /// </summary>
        public void AddCurrentSpeed()
        {
            int count = 0;
            for (int i = 0; i < sp1.Controls.Count; i++)
            {
                if (sp1.Controls[i] is CurrentSpeeds)
                {
                    count++;
                }
            }

            if (count < 5)
            {
                CurrentSpeeds cs = new CurrentSpeeds();
                cs.InitValue(count, DeviceSelectNo);
                sp1.Controls.Add(cs);
            }
            else
            {
                Common.ShowMessage("已超过添加流速的最大数量");
            }
        }

        /// <summary>
        /// 删除流速
        /// </summary>
        public void DeleteCurrentSpeed()
        {
            int count = 0;
            for (int i = 0; i < sp1.Controls.Count; i++)
            {
                if (sp1.Controls[i] is CurrentSpeeds)
                {
                    count++;
                }
            }

            if (count > 2)
            {
                sp1.Controls.RemoveAt(sp1.Controls.Count - 1);
            }
            else
            {
                Common.ShowMessage("已超过删除流速的最大数量");
            }
        }

        /// <summary>
        /// 删除设备
        /// </summary>
        private void btnClose_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除设备?", "删除设备", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                Control c = this.Parent;
                c.Controls.Remove(this);
                mf.InitFlowFs();
            }
        }

        private void gc_CustomButtonClick(object sender, DevExpress.XtraBars.Docking2010.BaseButtonEventArgs e)
        {
            if (e.Button.Properties.Caption == "添加流速仪")
            {
                AddCurrentSpeed();
            }

            if (e.Button.Properties.Caption == "删除流速仪")
            {
                DeleteCurrentSpeed();
            }
        }
    }
}
